/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import React from "react";
import { connect } from "dva";
import { Form, Table, Row, Col, Button } from "antd";
import { useAntdTable } from "ahooks";
import { triggerDownloadFile } from "@/utils/utils";

const Setting = props => {
  const { dispatch } = props;
  const getTableData = (res, formData) => {
    return dispatch({
      type: "order_calculate/getDownloadList",
      payload: {
        ...res,
        ...formData,
        page: res.current,
      },
    }).then(ress => ({
      total: ress.total,
      list: ress.list,
    }));
  };
  const { tableProps, search } = useAntdTable(getTableData, {
    defaultPageSize: 5,
  });
  const { reset } = search;
  const columns = [
    {
      title: "生成时间",
      dataIndex: "update_at",
      key: "update_at",
      align: "center",
    },
    {
      title: "文件",
      dataIndex: "file_name",
      key: "file_name",
      align: "center",
    },
    {
      title: "操作",
      dataIndex: "operanor",
      key: "operanor",
      align: "center",
      width: "20%",
      render: (_, { file_path, is_finish }) =>
        file_path && is_finish == 1 ? <a onClick={onDownload.bind(null, file_path)}>下载</a> : "--",
    },
  ];
  const onDownload = path => {
    if (path) {
      triggerDownloadFile(`https://upload.kuaidihelp.com${path}`);
    }
  };

  return (
    <>
      <Table columns={columns} rowKey="id" {...tableProps} />
      <Row style={{ position: "absolute", bottom: "15px" }}>
        <Col>
          <Button type="primary" onClick={reset}>
            刷新
          </Button>
        </Col>
      </Row>
    </>
  );
};

export default connect(result => {
  const { loading, order_calculate } = result;
  return {
    downListLoading: loading.effects["order_calculate/getDownloadList"],
    order_calculate,
  };
})(Form.create()(Setting));
